package com.panshi.service.impl;

import com.panshi.entity.AppComment;
import com.panshi.entity.AppPraiseTread;
import com.panshi.mapper.PraiseTreadMapper;
import com.panshi.service.CommentService;
import com.panshi.service.FeignAnswerApiClient;
import com.panshi.service.PraiseTreadService;
import com.panshi.tools.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PraiseTreadServiceImpl implements PraiseTreadService {

    @Autowired
    private PraiseTreadMapper praiseTreadMapper;

    @Autowired
    private CommentService commentService;

    @Autowired // 回答客户端
    private FeignAnswerApiClient feignAnswerApiClient;

    @Override
    public DataResult estimate(AppPraiseTread praiseTread) {
        System.out.println(praiseTread);
        DataResult result = new DataResult();
        AppPraiseTread app = praiseTreadMapper.estimate(praiseTread);
        if(null == app){ // 没有点赞、踩
            if(null != praiseTread.getPtStatus()){ // 用户执行回答点赞或点踩
                // 调用db，增加点赞数据
               this.addPraiseTread(praiseTread);
                if("1".equals(praiseTread.getPtStatus())) { // 回答表增加点赞量
                    feignAnswerApiClient.updatePariseTread(praiseTread.getTypeId(),"p");
                }else if("2".equals(praiseTread.getPtStatus())){ // 回答表增加点踩量
                    feignAnswerApiClient.updatePariseTread(praiseTread.getTypeId(),"t");
                }

            }else{ // 用户执行回答评论点赞，评论只有点赞
                praiseTread.setPtStatus("1");
                AppComment comment = commentService.queryCommentInfo(praiseTread.getTypeId());
                // 调用db
                commentService.updateParise(comment.getId());
                this.addPraiseTread(praiseTread);
            }
            result.setCode(praiseTread.getTypeId()).setMsg("点赞/点踩成功");
        }else if("1".equals(app.getPtStatus())){ // 已点赞
            result.setCode(1).setMsg("已点赞");
        }else if("2".equals(app.getPtStatus())){ // 已点踩
            result.setCode(2).setMsg("已点踩");
        }
        return result;
    }


    public Integer addPraiseTread(AppPraiseTread praiseTread) {
        praiseTreadMapper.addPraiseTread(praiseTread);
        return praiseTread.getPtId();
    }

}
